{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "558ed851-0244-4f62-9de8-47c143926e7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import subprocess\n",
    "import os\n",
    "\n",
    "result = subprocess.run('bash -c \"source /etc/network_turbo && env | grep proxy\"', shell=True, capture_output=True, text=True)\n",
    "output = result.stdout\n",
    "for line in output.splitlines():\n",
    "    if '=' in line:\n",
    "        var, value = line.split('=', 1)\n",
    "        os.environ[var] = value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f068ae87-be68-466c-abf0-8d6634d3ff4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasets import load_dataset\n",
    "\n",
    "ds = load_dataset(\"agupte/MedVQA\",cache_dir='/root/autodl-tmp/dataset/medical-vqa')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "877dc55d-465a-439f-a78d-0892f82d45e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatasetDict({\n",
       "    train: Dataset({\n",
       "        features: ['ids', 'image_names', 'images', 'questions', 'answers'],\n",
       "        num_rows: 635\n",
       "    })\n",
       "    test: Dataset({\n",
       "        features: ['ids', 'image_names', 'images', 'questions', 'answers'],\n",
       "        num_rows: 159\n",
       "    })\n",
       "})"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "11afb222-3b2c-4860-96af-eddd49162dc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({'ids': '0',\n",
       "  'image_names': 'synpic26764.jpg',\n",
       "  'images': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1024x1192>,\n",
       "  'questions': 'Is this film taken in a PA modality?',\n",
       "  'answers': 'Yes'},\n",
       " {'ids': '635',\n",
       "  'image_names': 'synpic13385.jpg',\n",
       "  'images': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=987x1200>,\n",
       "  'questions': 'Where are the most infiltrates located?',\n",
       "  'answers': 'Left lung'})"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ds['train'][0],ds['test'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "af31d47f-f94e-4d64-95a1-fe554e125f7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data has been processed and saved to 'processed_train_data.json' and 'processed_test_data.json'\n"
     ]
    }
   ],
   "source": [
    "from PIL import Image\n",
    "import os\n",
    "import json\n",
    "\n",
    "def process_item(item, index, base_dir='image'):\n",
    "    if not os.path.exists(base_dir):\n",
    "        os.makedirs(base_dir)\n",
    "    \n",
    "    image_path = os.path.join(base_dir, f'{index}.jpg')\n",
    "    item['images'].save(image_path)  # 保存图像\n",
    "    \n",
    "    return {\n",
    "        \"messages\": [\n",
    "            {\n",
    "                \"content\": f\"<image>{item['questions']}\",\n",
    "                \"role\": \"user\"\n",
    "            },\n",
    "            {\n",
    "                \"content\": item['answers'],\n",
    "                \"role\": \"assistant\"\n",
    "            }\n",
    "        ],\n",
    "        \"images\": [image_path]\n",
    "    }\n",
    "\n",
    "\n",
    "processed_train_data = []\n",
    "for i, item in enumerate(ds['train']):\n",
    "    processed_train_data.append(process_item(item, i, base_dir='image/train'))\n",
    "\n",
    "processed_test_data = []\n",
    "for i, item in enumerate(ds['test']):\n",
    "    processed_test_data.append(process_item(item, i, base_dir='image/test'))\n",
    "\n",
    "# 将处理后的训练数据保存到一个JSON文件\n",
    "with open('processed_train_data.json', 'w') as file:\n",
    "    json.dump(processed_train_data, file, indent=4)\n",
    "\n",
    "# 将处理后的测试数据保存到另一个JSON文件\n",
    "with open('processed_test_data.json', 'w') as file:\n",
    "    json.dump(processed_test_data, file, indent=4)\n",
    "\n",
    "print(\"Data has been processed and saved to 'processed_train_data.json' and 'processed_test_data.json'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a123af99-97d3-4220-b3e1-d5041da2c11c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
